<template>
  <div class="app-container">
    <div>
      <el-button style="margin-bottom: 10px;" icon="el-icon-plus" type="primary" @click="handleAdd()">添加</el-button>
      <el-table v-loading="loading" ref="multipleTable" border :data="infoData">
        <el-table-column label="序号" width="80">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column property="title" label="图片" show-overflow-tooltip>
          <template slot-scope="scope">
          <img :src="$S.path + scope.row.imgPath" width="80px" height="80px" alt="">
        </template>
        </el-table-column>
        <el-table-column property="imgPath" label="路径" show-overflow-tooltip>
        </el-table-column>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
            <el-button size="mini" @click="removeucAvatar(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <edit v-if="edit" :data="selectTable" @confirmEvent="confirmEvent"></edit>
  </div>
</template>
<script>
import singleImage from '@/components/Upload/singleImage'
import edit from './edit'
import * as api from '@/utils/api'
export default {
  components: {
    singleImage,
    edit
  },
  data() {
    return {
      infoData: [],
      curEditIndex: null,
      edit: false,
      loading: false
    }
  },
  created() {
    this.getucAvatar()
  },
  methods: {
    async getucAvatar() {
      try {
        const res = await api.getucAvatar()
        this.infoData = res
      } catch (err) {
        console.log(err)
      }
    },
    handleAdd() {
      this.selectTable = {}
      this.selectTable.method = 'add'
      this.edit = true
    },
    async removeucAvatar(index, row) {
      this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
        try {
          this.pload = false
          await api.removeucAvatar({ id: row.id })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getucAvatar()
        } catch (e) {
          this.pload = true
        }
      })
    },
    confirmEvent(data) {
      if (data.method === 'add') {
        this.adducAvatar({ imgPath: data.imgPath })
      }
    },
    async adducAvatar(data) {
      try {
        await api.adducAvatar(data)
        this.getucAvatar()
        this.$notify({ title: '保存成功', type: 'success', duration: 3000 })
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
